﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Connectivity;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace KoolearnSample.Controls
{
    /// <summary>
    /// 判断当前是否能够连接网络
    /// </summary>
    public sealed partial class KoolearnNetwork : UserControl
    {
        #region Construction
        public KoolearnNetwork()
        {
            this.InitializeComponent();
            if (!IsConnectedToInternet())
            {
                var bitmap = new BitmapImage(new Uri("ms-appx:///Assets/wifi_cross.png"));
                ImageStatus.Source = bitmap;
                ImageStatus.SetValue(ToolTipService.ToolTipProperty, "网络不给力啊亲");
            }
        }
        #endregion

        #region Method
        private bool IsConnectedToInternet()
        {
            bool connected = false;
            var cp = NetworkInformation.GetInternetConnectionProfile();
            if (cp != null)
            {
                var cl = cp.GetNetworkConnectivityLevel();
                connected = cl == NetworkConnectivityLevel.InternetAccess;
            }
            return connected;
        }
        #endregion
    }
}
